package com.phonemap.service.server;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.google.appengine.api.datastore.Entity;
import com.phonemap.common.Util;
import com.phonemap.models.Photo;
import com.phonemap.models.Position;
import com.phonemap.models.User;

@WebService
public class PhonemapAPI {
	private static final Logger logger = Logger.getLogger(PhonemapAPI.class
			.getCanonicalName());
	@WebMethod
	public boolean createOrUpdateUser(
			@WebParam(name = "phone", targetNamespace = "") String phone,
			@WebParam(name = "firstname", targetNamespace = "") String firstname,
			@WebParam(name = "lastname", targetNamespace = "") String lastname,
			@WebParam(name = "email", targetNamespace = "") String email,
			@WebParam(name = "password", targetNamespace = "") String password,
			@WebParam(name = "avatar", targetNamespace = "") String avatar) {
		logger.log(Level.INFO, "creating position");
		if (User.createOrUpdateUser(phone, firstname, lastname, email,
				password, avatar) != null)
			return true;
		else
			return false;
	}

	@WebMethod
	public boolean createOrUpdatePosition(
			@WebParam(name = "userId", targetNamespace = "") String userId,
			@WebParam(name = "latitude", targetNamespace = "") Double latitude,
			@WebParam(name = "longitude", targetNamespace = "") Double longitude) {
		logger.log(Level.INFO, "creating or updating item");
		if (Position.createOrUpdatePosition(userId, latitude, longitude) != null)
			return true;
		else
			return false;
	}

	@WebMethod
	public String deleteUser(
			@WebParam(name = "phone", targetNamespace = "") String phone) {
		logger.log(Level.INFO, "deleting position");
		return User.deleteUser(phone);
	}

	@WebMethod
	public String deletePosition(
			@WebParam(name = "userId", targetNamespace = "") String userId) {
		logger.log(Level.INFO, "deleting item");
		return Position.deletePosition(userId);
	}

	@WebMethod
	public String getAllUsers(
			@WebParam(name = "kind", targetNamespace = "") String kind) {
		Iterable<Entity> entities = null;
		entities = User.getAllUsers(kind);
		return Util.writeJSON(entities);
	}

	@WebMethod
	public String getUser(
			@WebParam(name = "phone", targetNamespace = "") String phone) {
		Entity position = User.getUser(phone);
		if (position != null) {
			Set<Entity> resultSet = new HashSet<Entity>();
			resultSet.add(position);
			return Util.writeJSON(resultSet);
		}
		return null;
	}

	@WebMethod
	public String getAllPositions(
			@WebParam(name = "kind", targetNamespace = "") String kind) {
		Iterable<Entity> entities = null;
		entities = Position.getAllPositions(kind);
		return Util.writeJSON(entities);
	}

	@WebMethod
	public String getPosition(
			@WebParam(name = "userId", targetNamespace = "") String userId) {
		Iterable<Entity> entities = null;
		entities = Position.getPosition(userId);
		return Util.writeJSON(entities);
	}

	@WebMethod
	public String getPositionsForUser(
			@WebParam(name = "kind", targetNamespace = "") String kind,
			@WebParam(name = "userId", targetNamespace = "") String userId) {
		Iterable<Entity> entities = null;
		entities = Position.getPositionsForUser(kind, userId);
		return Util.writeJSON(entities);
	}
	
	@WebMethod
	public boolean createOrUpdatePhoto(@WebParam(name = "userId", targetNamespace = "") String userId, @WebParam(name = "title", targetNamespace = "") String title,
			@WebParam(name = "caption", targetNamespace = "") String caption,  @WebParam(name = "contentType", targetNamespace = "") String contentType, @WebParam(name = "photoData", targetNamespace = "") byte[] photoData, @WebParam(name = "photoPath", targetNamespace = "")String photoPath) {
		logger.log(Level.INFO, "creating Photo");
		if (Photo.createOrUpdatePhoto(userId, title, caption, contentType, photoData, photoPath) != null)
			return true;
		else
			return false;
	}
}
